﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Drawing;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class hospitalAmountSubsidyStatistics3 :BasePage
    {
        DataTable dtPSbsidyAmount;
        DataTable dtRatio;
        DataTable dtPtotalAmount;
        int state = 1;//显示的图片状态(0:线状,1:柱状,2:饼状)
        int a = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            txtHosName.Attributes.Add("readonly", "readonly");
            if (!IsPostBack)
            {
                if (Request["state"] != null)
                {
                    state = int.Parse(Request["state"]);
                }


                BindArea();
                dataBind(state);
                //HidSort.Value = sort.ToString();
                HidState.Value = state.ToString();
            }
            else
            {
                // sort = int.Parse(HidSort.Value);
                state = int.Parse(HidState.Value);
            }


        }

        /// <summary>    
        /// 查询数据 type:0:医院已确认补助,1:已补助
        /// </summary>
        private void dataBind(int t)
        {
            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
           // StringBuilder sql = new StringBuilder();
            string sqlgroup = "";
            StringBuilder sqlwhere = new StringBuilder();
            if (!string.IsNullOrEmpty(this.HidHosId.Value))
            {
                sqlwhere.Append(" and PHospitalID = '" + this.HidHosId.Value + "' ");
            }
            if (ddlCity.SelectedIndex != 0)
            {

                if (ddlCity.SelectedValue.ToString().Equals("530101"))
                {
                    sqlwhere.Append(" and PAreaId = '530100'");
                }
                else
                {
                    sqlwhere.Append(" and PAreaId like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
                }
            }
            if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            {
                sqlwhere.Append(" and PSubsidyTime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
            }
            if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            {
                sqlwhere.Append(" and PSubsidyTime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
            }

            string sql1 = "select top 10 ProcureCatalogID,max(ProductName) as ProductName ,SUM(PSubsidiesAmount) as PSbsidyAmount_Order  from view_Prescription where POrderStatus>0" + sqlwhere.ToString() + " group by ProcureCatalogID having SUM(PSubsidiesAmount)>0  order by PSbsidyAmount_Order desc";


            string sql2 = "select top 10 ProcureCatalogID,MAX(ProductName) ProductName,SUM(Price*Pcount) PtotalAmount,(SELECT SUM(PtotalAmount) FROM TB_Prescription) PtotalAmount,SUM(Price*Pcount)/(SELECT SUM(PtotalAmount)  FROM   TB_Prescription) Ratio FROM   TB_Prescriptiondetail a, TB_Prescription b WHERE a.PrescriptionId=b.PrescriptionId AND b.POrderStatus<>0 " + sqlwhere.ToString() + " GROUP BY ProcureCatalogID having SUM(Price*Pcount)/(SELECT SUM(PtotalAmount)  FROM   TB_Prescription)>0 order by ratio desc";

            string sql3 = "select top 10 ProcureCatalogID,max(ProductName) as ProductName ,sum(Pamount) as Pamount from view_Prescription where POrderStatus>0  " + sqlwhere.ToString() + " group by ProcureCatalogID having sum(Pamount)>0 order by Pamount desc";

            dtPSbsidyAmount = iPrescription.GetSubsidyStatistics(sql1);
            dtRatio = iPrescription.GetSubsidyStatistics(sql2);
            dtPtotalAmount = iPrescription.GetSubsidyStatistics(sql3);

            BindChart(t);
        }
        /// <summary>
        /// 绑定地区
        /// </summary>
        public void BindArea()
        {
            IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
            IList<Area> lstArea = new List<Area>();
            string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
            //判断用户所属地区，如果用户是省级的 则可以列出全部区域
            //if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
            //{
            int i;
            lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
            foreach (Area area in lstArea)
            {
                ListItem ddlitem = new ListItem();
                ddlitem.Text = area.AreaName;
                ddlitem.Value = area.AreaID;
                ddlCity.Items.Add(ddlitem);
            }

            ListItem b = new ListItem();
            b.Text = "全部";
            b.Value = "";
            ddlCity.Items.Insert(0, b);


        }
        /// <summary>
        /// 图表绑定 (初始)
        /// </summary>
        private void BindChart(int type)
        {

            //空为初始页
            //string AreaName = "";//月份
            //int count = 2;//次数
            //string sort = "0";//属性 0:基药 1:非基药
            SeriesChartType t;
            switch (type)
            {
                case 0:
                    t = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                    break;
                case 1:
                    t = SeriesChartType.Column;
                    break;
                case 2:
                    t = SeriesChartType.Pie;
                    break;
                default:
                    t = SeriesChartType.Column;
                    break;
            }



            #region 图

            //***************** 已补贴金额 单位：万元*****************/
            Series series1 = new Series("补贴总金额(万元)");
            series1.ChartType = t;
            series1.BorderWidth = 1;
            series1.ShadowOffset = 1;
            //***************** 处方单金额*******************/
            Series series2 = new Series("药品使用率(%)");
            series2.ChartType = t; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
            series2.BorderWidth = 1;
            series2.ShadowOffset = 1;
            //***************** 处方单金额*******************/
            Series series3 = new Series("药品使用金额(万元)");
            series3.ChartType = t; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
            series3.BorderWidth = 1;
            series3.ShadowOffset = 1;
            if (type == 2)
            {
                series1["PieDrawingStyle"] = "Concave";
                series2["PieDrawingStyle"] = "Concave";
                series1.Label = "#PERCENT{P1}";
                series2.Label = "#PERCENT{P1}";
            }
            else if (type == 1)
            {
                series1["PointWidth"] = "0.3";
                series2["PointWidth"] = "0.3";
                series3["PointWidth"] = "0.3";
                series1["DrawingStyle"] = "Emboss";
                series2["DrawingStyle"] = "Emboss";
                series3["DrawingStyle"] = "Emboss";

            }
            if (dtPSbsidyAmount.Rows.Count > 0)
            {
                this.hospitalAmountSubsidy1.Visible = true;
                for (int i = 0; i < dtPSbsidyAmount.Rows.Count; i++) //添加数据
                {

                    series1.Points.AddXY(i + 1, (double.Parse(dtPSbsidyAmount.Rows[i]["PSbsidyAmount_Order"].ToString()) / 10000).ToString("0.0000"));
                    series1.Points[i].ToolTip = dtPSbsidyAmount.Rows[i]["ProductName"].ToString();
                   

                }
            }
            else
            {
                //this.ChartInStorage.Visible = false;
                this.hospitalAmountSubsidy1.Visible = false;
                //lblnull.Visible = true;
            }

            if (dtRatio.Rows.Count > 0)
            {
                this.hospitalAmountSubsidy2.Visible = true;
                for (int i = 0; i < dtRatio.Rows.Count; i++) //添加数据
                {
                    series2.Points.AddXY(i + 1, (double.Parse(dtRatio.Rows[i]["Ratio"].ToString())*100).ToString("0.00"));
                    series2.Points[i].ToolTip = dtRatio.Rows[i]["ProductName"].ToString();
                }
            }
            else
            {
                this.hospitalAmountSubsidy2.Visible = false;
            }


            if (dtPtotalAmount.Rows.Count > 0)
            {  this.hospitalAmountSubsidy3.Visible = true;

                for (int i = 0; i < dtPtotalAmount.Rows.Count; i++) //添加数据
                {
                    series3.Points.AddXY(i + 1, (double.Parse(dtPtotalAmount.Rows[i]["Pamount"].ToString())/10000).ToString("0.0000"));
                    series3.Points[i].ToolTip = dtPtotalAmount.Rows[i]["ProductName"].ToString();
                }
            }
            else
            {
                this.hospitalAmountSubsidy3.Visible = false;
            }
            #endregion




            #region 数据绑定到图
            //左图
            series1.IsValueShownAsLabel = true;
            hospitalAmountSubsidy1.Series.Add(series1);
            series2.IsValueShownAsLabel = true;
            hospitalAmountSubsidy2.Series.Add(series2);
            series3.IsValueShownAsLabel = true;
            hospitalAmountSubsidy3.Series.Add(series3);
            #endregion

            #region 两张图片属性
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.IsMarginVisible = true;
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].Area3DStyle.Enable3D = false;
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.TextOrientation = TextOrientation.Horizontal;
            //hospitalAmountSubsidy.Series["hospitalAmountSubsidy"]["hospitalAmountSubsidy"] = PointLabelsList.SelectedItem.Text;
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.LineWidth = 1;
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.IntervalOffset = 1;

            hospitalAmountSubsidy1.Series["补贴总金额(万元)"].MarkerStyle = MarkerStyle.Circle;
            hospitalAmountSubsidy2.Series["药品使用率(%)"].MarkerStyle = MarkerStyle.Square;
            hospitalAmountSubsidy3.Series["药品使用金额(万元)"].MarkerStyle = MarkerStyle.Square;

            hospitalAmountSubsidy1.Titles.Add("补贴总金额排名图");
            hospitalAmountSubsidy1.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
            hospitalAmountSubsidy1.Titles[0].Position.X = 30;
            hospitalAmountSubsidy1.Titles[0].Position.Y = 1;
            hospitalAmountSubsidy1.Titles[0].Position.Width = 50;
            hospitalAmountSubsidy1.Titles[0].Position.Height = 8;
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisY.Title = "金额(万元)";
            hospitalAmountSubsidy1.ChartAreas["hospitalAmountSubsidy1"].AxisX.Title = "名次";


            hospitalAmountSubsidy2.Titles.Add("药品使用率排名图");
            hospitalAmountSubsidy2.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
            hospitalAmountSubsidy2.Titles[0].Position.X = 30;
            hospitalAmountSubsidy2.Titles[0].Position.Y = 1;
            hospitalAmountSubsidy2.Titles[0].Position.Width = 50;
            hospitalAmountSubsidy2.Titles[0].Position.Height = 8;
            hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisY.Title = "比例(%)";
            hospitalAmountSubsidy2.ChartAreas["hospitalAmountSubsidy2"].AxisX.Title = "名次";

            hospitalAmountSubsidy3.Titles.Add("药品使用金额排名图");
            hospitalAmountSubsidy3.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
            hospitalAmountSubsidy3.Titles[0].Position.X = 30;
            hospitalAmountSubsidy3.Titles[0].Position.Y = 1;
            hospitalAmountSubsidy3.Titles[0].Position.Width = 50;
            hospitalAmountSubsidy3.Titles[0].Position.Height = 8;
            hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisY.Title = "金额(万元)";
            hospitalAmountSubsidy3.ChartAreas["hospitalAmountSubsidy3"].AxisX.Title = "名次";
            //if (series2.Name == "数量")
            //{
            //    hospitalAmountSubsidy.Series["金额"].YAxisType = AxisType.Secondary;
            //    hospitalAmountSubsidy0.Series["数量"].YAxisType = AxisType.Primary;
            //}
            #endregion



        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            dataBind(state);
        }

        protected void HzButton3_Click(object sender, EventArgs e)
        {
            HidState.Value = "2";
            state = 2;
            dataBind(state);
        }

        protected void HzButton2_Click(object sender, EventArgs e)
        {

            HidState.Value = "0";
            state = 0;
            dataBind(state);
        }

        protected void HzButton1_Click(object sender, EventArgs e)
        {
            HidState.Value = "1";
            state = 1;
            dataBind(state);
        }



        protected void HzButton6_Click(object sender, EventArgs e)
        {

            dataBind(state);
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
          
        //    dataBind(state);
        //    this.hospitalAmountSubsidy1.Visible = true;
        //    this.hospitalAmountSubsidy2.Visible = false;
        //}

        //protected void Button2_Click(object sender, EventArgs e)
        //{

        //    dataBind(state);
        //    this.hospitalAmountSubsidy1.Visible = false;
        //    this.hospitalAmountSubsidy2.Visible = true;
        //}
    }
}
